const { mongoose, db } = require('../utils/mongoose')
let schema = mongoose.Schema({
    id:{type:String,required:true},
    start:{type:Date,required:true},
    end:{type:Date,required:false},
    time:{type:Number,required:false},
    place:{type:String,required:false},
    mileage:{type:Number,required:false},
})

let Collection = mongoose.model('exercises', schema)


//新增
const addExercise=(data)=>{
    let model = new Collection(data)
    return model.save()
        .then(res => res)
        .catch(err => {
            console.log(err);
            return false;
        })
}
//查询
// .sort({'date':-1})//按插入时间倒序排列
// date:{$gte: sDate, $lte: eDate}
const findExercise=({id,start,end})=>{
    return Collection.find({id,start:{$gte: start,$lte: end}}).sort({'_id':-1})
        .then(res=>res)
        .catch(err=>{
            console.log(err);
            return false;
        })
}
module.exports={
    addExercise,
    findExercise,
}